
import DatabaseConnection;
import Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentManagementGUI extends JFrame {
    private JTable table;
    private DefaultTableModel tableModel;
    private JTextField studentIdField, nameField, genderField, birthDateField, politicalStatusField, addressField, phoneField, dormNumberField;

    public StudentManagementGUI() {
        setTitle("学生信息管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建表格
        tableModel = new DefaultTableModel();
        tableModel.addColumn("学号");
        tableModel.addColumn("姓名");
        tableModel.addColumn("性别");
        tableModel.addColumn("出生日期");
        tableModel.addColumn("政治面貌");
        tableModel.addColumn("家庭住址");
        tableModel.addColumn("电话");
        tableModel.addColumn("宿舍号");
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 创建输入面板
        JPanel inputPanel = new JPanel(new GridLayout(9, 2));
        inputPanel.add(new JLabel("学号:"));
        studentIdField = new JTextField();
        inputPanel.add(studentIdField);
        inputPanel.add(new JLabel("姓名:"));
        nameField = new JTextField();
        inputPanel.add(nameField);
        inputPanel.add(new JLabel("性别:"));
        genderField = new JTextField();
        inputPanel.add(genderField);
        inputPanel.add(new JLabel("出生日期:"));
        birthDateField = new JTextField();
        inputPanel.add(birthDateField);
        inputPanel.add(new JLabel("政治面貌:"));
        politicalStatusField = new JTextField();
        inputPanel.add(politicalStatusField);
        inputPanel.add(new JLabel("家庭住址:"));
        addressField = new JTextField();
        inputPanel.add(addressField);
        inputPanel.add(new JLabel("电话:"));
        phoneField = new JTextField();
        inputPanel.add(phoneField);
        inputPanel.add(new JLabel("宿舍号:"));
        dormNumberField = new JTextField();
        inputPanel.add(dormNumberField);

        // 创建按钮
        JButton addButton = new JButton("添加");
        JButton updateButton = new JButton("更新");
        JButton deleteButton = new JButton("删除");
        JButton queryButton = new JButton("查询");

        inputPanel.add(addButton);
        inputPanel.add(updateButton);
        inputPanel.add(deleteButton);
        inputPanel.add(queryButton);

        add(inputPanel, BorderLayout.SOUTH);

        // 添加按钮事件
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addStudent();
            }
        });

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateStudent();
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteStudent();
            }
        });

        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryStudents();
            }
        });

        // 初始化查询
        queryStudents();
    }

    private void addStudent() {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "INSERT INTO students (student_id, name, gender, birth_date, political_status, address, phone, dorm_number) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, studentIdField.getText());
            stmt.setString(2, nameField.getText());
            stmt.setString(3, genderField.getText());
            stmt.setString(4, birthDateField.getText());
            stmt.setString(5, politicalStatusField.getText());
            stmt.setString(6, addressField.getText());
            stmt.setString(7, phoneField.getText());
            stmt.setString(8, dormNumberField.getText());
            stmt.executeUpdate();
            JOptionPane.showMessageDialog(this, "学生信息添加成功！");
            queryStudents();
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "添加失败：" + ex.getMessage());
        }
    }

    private void updateStudent() {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "UPDATE students SET name=?, gender=?, birth_date=?, political_status=?, address=?, phone=?, dorm_number=? WHERE student_id=?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, nameField.getText());
            stmt.setString(2, genderField.getText());
            stmt.setString(3, birthDateField.getText());
            stmt.setString(4, politicalStatusField.getText());
            stmt.setString(5, addressField.getText());
            stmt.setString(6, phoneField.getText());
            stmt.setString(7, dormNumberField.getText());
            stmt.setString(8, studentIdField.getText());
            stmt.executeUpdate();
            JOptionPane.showMessageDialog(this, "学生信息更新成功！");
            queryStudents();
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "更新失败：" + ex.getMessage());
        }
    }

    private void deleteStudent() {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "DELETE FROM students WHERE student_id=?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, studentIdField.getText());
            stmt.executeUpdate();
            JOptionPane.showMessageDialog(this, "学生信息删除成功！");
            queryStudents();
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "删除失败：" + ex.getMessage());
        }
    }

    private void queryStudents() {
        tableModel.setRowCount(0); // 清空表格
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT * FROM students";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                tableModel.addRow(new Object[]{
                        rs.getString("student_id"),
                        rs.getString("name"),
                        rs.getString("gender"),
                        rs.getString("birth_date"),
                        rs.getString("political_status"),
                        rs.getString("address"),
                        rs.getString("phone"),
                        rs.getString("dorm_number")
                });
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StudentManagementGUI().setVisible(true);
            }
        });
    }
}